package student.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.util.MyLog;
import com.dao.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

//import edu.wfmanage.models.Lookup;
import edu.student.models.*;

/**
 * 
 * @author Ruchika Sharma
 */

public class SearchRollResult extends ActionSupport {
    
	private static final long serialVersionUID = 1L;
	private String commandButton = "";
	private String searchBox;
	private String rollNo;
	static String fRollNo;
	 private String name;
	 private String dob;
	 private boolean friends;
	 private User friend = null;
	 private Friend selection=null;
	 private String username="";
		private List<News> news;
		private List<Announcements> announcements;
		private String image="";
		private Timestamp lastLogin;
	 public Friend getSelection() {
		return selection;
	}

	public void setSelection(Friend selection) {
		this.selection = selection;
	}

	private ArrayList<Interest> interests;
	public String getRollNo() {
		return rollNo;
	}

	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDob() {
		return dob;
	}

	public void setDob(String dob) {
		this.dob = dob;
	}
	

	
	public boolean isFriends() {
		return friends;
	}

	public void setFriends(boolean friends) {
		this.friends = friends;
	}

	public User getFriend() {
		return friend;
	}

	public void setFriend(User friend) {
		this.friend = friend;
	}

	public ArrayList<Interest> getInterests() {
		return interests;
	}

	public void setInterests(ArrayList<Interest> interests) {
		this.interests = interests;
	}

	public String getCommandButton() {
		return commandButton;
	}

	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	
    
	public String getSearchBox() {
		return searchBox;
	}

	public void setSearchBox(String searchBox) {
		this.searchBox = searchBox;
	}

	public String execute() {
		MyLog.log("in Login.execute() with commandButton = " + 
    	        this.commandButton);
		System.out.println("In login action");
		try{
			/*if(searchBox!="" && commandButton.equals("search"))
			System.out.println("searchBox value is"+searchBox);
			else System.out.println("sorry searchBox value is "+searchBox);*/
			User user=new User();
			Map<String, Object> session = ActionContext.getContext().getSession();
			
			if(session.get("user") != null && session.get("user") != "")
				user = (User) session.get("user");
			session.put("user", user);
				rollNo = user.getRollNor();
				username=user.getUsername();
				image=user.getImage();
				lastLogin=user.getLastLogin();
				news = News.getNews(" where ui.roll_No='"+user.getRollNor()+"' ");
				announcements=Announcements.getAnnouncements(" where ui.roll_No='"+user.getRollNor()+"' ");
				fRollNo=searchBox;
				System.out.println("RollNo "+rollNo);
				System.out.println("search value "+searchBox);
				friend= User.findone("Select * from user where ROLL_NO ="+"'"+searchBox+"'");
				System.out.println("friend is" + friend.getRollNor());
				if(friend.getRollNor()=="")
				{System.out.println("roll nor is null");
					addActionError("No such user exists");
					return "failure";
				}
				friend.setInterests(Interest.getInterests(" where u.ROLL_NO='"+searchBox+"' "));
				//System.out.println("interest is" + friend.getInterests());
				selection = FriendDao.findOne("where ROLL_NO = '"+rollNo+"' AND FRIEND_ROLL_NO = '"+searchBox+"'");
				//System.out.println("friend roll no is  "+selection.getFriendRollNo());
				if(selection.getFriendRollNo()!=null)
				{System.out.println("selection is not null");
					friends = true;}
				else{System.out.println("selection is null");
				friends=false;
				}
				
				
			return "success";
       
		} catch (Exception e) {
			MyLog.log("Error while checking credentials from database"+e);
		}
		return "success";

    }
	 
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<News> getNews() {
		return news;
	}

	public void setNews(List<News> news) {
		this.news = news;
	}

	public List<Announcements> getAnnouncements() {
		return announcements;
	}

	public void setAnnouncements(List<Announcements> announcements) {
		this.announcements = announcements;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Timestamp getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Timestamp lastLogin) {
		this.lastLogin = lastLogin;
	}
	 
    
        
   
}
